<template>
	<view class="pages-home">
		<view class="item pt-sm pl-lg pr-lg">
			<view class="h-88 f-mini-title flex-y-center text-bold">
				<text class="c-warning">*</text>
				<text>主订单号</text>
			</view>
			<view class="h-110 radius-10 fill-body flex-y-center">
				<text class="f-paragraph pl-lg pr-lg">{{ detail.new_order_code}}</text>
			</view>
		</view>
		<view class="item pt-sm pl-lg pr-lg">
			<view class="h-88 f-mini-title flex-y-center text-bold">
				<text class="c-warning">*</text>
				<text>补单金额（元）</text>
			</view>
			<view class="h-110 radius-10 fill-body">
				<input v-model="param.price" :disabled="options.is_add == 1" type="digit" class="h-110 pl-lg pr-lg" placeholder="请输入补单金额" />
			</view>
		</view>
		<view class="item pt-sm pl-lg pr-lg">
			<view class="h-88 f-mini-title flex-y-center text-bold">
				<text class="c-warning">*</text>
				<text>补单类型</text>
			</view>
			<view class="radius-10">
				<view class="flex-y-center h-80" v-for="(item,index) in list" :key="index" @tap="changeText(index)">
					<i class="iconfont" :style="{color: param.text == item ? primaryColor : '#ccc'}"
					:class="[{'icon-radio-fill': param.text == item},{'icon-xuanze': param.text != item}]"></i>
					<text class="pl-sm">{{item}}</text>
				</view>
			</view>
		</view>
		<view class="item pt-sm pl-lg pr-lg">
			<view class="h-88 f-mini-title flex-y-center">
				<text class="text-bold">申请补单说明</text>
			</view>
			<view class="flex radius-10 fill-body">
				<textarea v-model="param.text_info" :disabled="options.is_add == 1" class="flex-1 pd-lg" name="" id="" cols="30" rows="10" placeholder="请输入"></textarea>
			</view>
		</view>
		<view class="space-max-footer"></view>
		<fix-bottom-button @confirm="onSubmit" v-if="options.is_add != 1"
			:text="[{text:'申请补差价',type:'confirm'}]" bgColor="#fff" borderRadius="10rpx">
		</fix-bottom-button>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	import siteInfo from '@/siteinfo.js';
	let timer = null
	export default {
		data() {
			return {
				options: {},
				list: ['超时补差价','超任务补差价','尾款（已付定金）','其他'],
				param: {
					id: '',
					price: '',
					text: '超时补差价',
					text_info: ''
				},
				detail: {}
			}
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			configInfo: state => state.config.configInfo,
			coachInfo: state => state.user.coachInfo,
		}),
		async onLoad(options) {
			this.param.id = options.id
			this.options = options
			this.$util.showLoading()
			this.initIndex()
		},
		methods: {
			...mapActions(['getConfigInfo', 'getCoachInfo']),
			...mapMutations(['updateHomeItem', 'updateUserItem']),
			async initIndex(refresh = false){
				if(!this.configInfo.id || refresh){
					await this.getConfigInfo()
				}
				this.$util.setNavigationBarColor({
					bg: this.primaryColor
				})
				await this.getOrderInfo()
				this.$util.hideAll()
			},
			async getOrderInfo(){
				let {
					id,
					type = 0,
					is_add = 0
				} = this.options
				console.log(is_add, '======> is_add')
				let data = ''
				if(type == 1){
					let mkey = {
						0: 'orderInfo',
						1: 'addOrderInfo'
					}
					data = await this.$api.order[mkey[Number(is_add)]]({id})
					data.new_order_code = is_add == 0 ? data.order_code : data.main_order_code
				}else{
					let mkey = {
						0: 'demandInfo',
						1: 'addOrderInfo'
					}
					data = await this.$api.demand[mkey[Number(is_add)]]({id})
					data.new_order_code = is_add == 0 ? data.order_code : data.main_order_code
					if(data.type == 1 && is_add == 0 ){
						this.param.text = '尾款（已付定金）'
					}
				}
				
				this.detail = data
				if(Number(is_add)){
					for(let key in this.param){
						this.param[key] = data[key] || '无'
					}
				}
			},
			changeText(index){
				if(this.options.is_add == 1) return
				this.param.text = this.list[index]
			},
			async onSubmit(){
				let { price } = this.param
				if(!price){
					this.$util.showToast({
						title: `请输入补单金额`
					})
					return
				}else if(Number(price) <= 0){
					this.$util.showToast({
						title: `补单金额必须大于0`
					})
					return
				}
				else if(!/^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/.test(price) || parseFloat(price) < 0.01){
					this.$util.showToast({
						title: `补单金额，最多2位小数`
					})
					return
				}
				let {
					type = 0
				} = this.options
				let mkey = {
					0: 'demand',
					1: 'order'
				}
				await this.$api[mkey[type]].addPrice(this.param)
				 this.$util.showToast({
				 	title: `申请成功`
				 })
				setTimeout(()=>{
					this.$util.back()
					this.$util.goUrl({url: 1 ,openType: 'navigateBack'})
				},1500)
			}
		},
	}
</script>


<style lang="scss">
	page{
		background: #fff;
	}
</style>
